<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Auth library
 *
 */
class Auth{
	var $CI = NULL;
	function __construct()
	{
		// get CI's object
		$this->CI =& get_instance();
	}
	 // untuk validasi login
	function do_login($username,$password)
	{
		// cek di database, ada ga? 
		$this->CI->db->from('adm_user');
		$this->CI->db->where('login_name',$username);
		$this->CI->db->where('password=MD5("'.$password.'")');
		$result = $this->CI->db->get();
		
		if($result->num_rows() == 0) 
		{
			// username dan password tsb tidak ada 
			return false;
		}
		else	
		{
			// ada, maka ambil informasi dari database
			$row = $result->row();
			$session_data = array(
				'SESS_USER_NAME' => $row->login_name,
				'SESS_PASSWORD' => $row->password,
				'SESS_NAMA_LENGKAP' => $row->nama,
				'LEVEL_GROUP' => $row->id_adm_group
			);
			// set session untuk user ini
			$this->CI->session->set_userdata($session_data);
			return true;
		}
	} 
	
	
	// untuk mengecek apakah user sudah login/belum
	function is_logged_in()
	{
		if($this->CI->session->userdata('SESS_USER_NAME') == '')
		{
			return false;
		}
		return true;
	}
	
		
	// untuk logout
	function do_logout()
	{
		$this->CI->session->sess_destroy();	
	}
	
	//untuk ambil menu dari database
	function get_menu($LevelUser){
		$query=$this->CI->db->query("SELECT m.idmenu AS id, m.text, m.page_name, m.parentid, m.subMenuWidth, m.classname, m.functionname
									FROM adm_menu AS m
									INNER JOIN  adm_group_menu AS gm ON gm.id_adm_menu=m.idmenu
									WHERE gm.id_adm_group=$LevelUser");
		if ($query->num_rows()>0){
			$data = array();
			$i=0;
			foreach ($query->result_array() AS $row){
				$row["id"];
				$row["text"];
				$row["page_name"];
				$row["parentid"];
				$row["subMenuWidth"];
				$row["classname"];
				$row["functionname"];
				
				$data[$i] = $row;
				$i++;
			}
			return $data;
				
		}
	} 
	
}	
	
	/* $query=$this->CI->db->query("SELECT idmenu AS id, concat(classname,'/',functionname) AS goto_function
									FROM adm_menu 
									INNER JOIN  adm_group_menu ON adm_group_menu.id_adm_menu=adm_menu.idmenu
									WHERE adm_group_menu.id_adm_group=$level");	*/
